package com.byx.chatserver.service.impl;

import com.byx.chatserver.dto.LoginDto;
import com.byx.chatserver.entity.Friend;
import com.byx.chatserver.entity.ResultCode;
import com.byx.chatserver.entity.SimpleFriend;
import com.byx.chatserver.entity.User;
import com.byx.chatserver.mapper.IUserMapper;
import com.byx.chatserver.service.INumberPoolService;
import com.byx.chatserver.service.IUserService;
import com.byx.chatserver.util.Base64Util;
import com.byx.chatserver.vo.LoginVo;
import com.byx.springboot.unifiedresponseresult.entity.ServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserServiceImpl
 *
 * @author Superb
 * @date 2021/2/7 21:11
 */
@Slf4j
@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private IUserMapper userMapper;

    /**
     * 登录
     *
     * @param loginVo
     * @return
     */
    @Override
    public LoginDto loginByVo(LoginVo loginVo) {
        LoginDto loginDto;
        //Base64解密，再到数据库比对
        loginVo.setPassword(Base64Util.decode(loginVo.getPassword()));
        if((loginDto=userMapper.login(loginVo))!=null){
            // 如果本地数据库与服务器数据库的最后一次改动时间不相同，就返回新的数据，否则返回空
            if (loginVo.getLastModifyTime() == null || !loginVo.getLastModifyTime().equals(loginDto.getLastModifyTime())){
                return loginDto;
            }
            return null;
        }
        throw new ServerException(ResultCode.USER_LOGIN_ERROR);
    }

    @Override
    public String registerByTel(String tel, String password, INumberPoolService numberPoolService) {
        if (numberPoolService != null){
            String loginId = numberPoolService.selectNumber();
            try{
                userMapper.registerByTel(loginId,tel,Base64Util.decode(password));
            } catch (Exception e) {
                log.error("registerByTel",e);
                throw new ServerException(ResultCode.FAILED);
            }
            //每有一个新用户注册，就提前检测号码池。运行在子线程中，避免阻塞主线程
            numberPoolService.execute();
            return loginId;
        }
        throw new ServerException(ResultCode.INTERNAL_SERVER_ERROR);
    }

    @Override
    public String registerByEmail(String email, String password, Long timestamp, INumberPoolService numberPoolService) {
        if (numberPoolService != null){
            String loginId = numberPoolService.selectNumber();
            if(loginId != null){
                try{
                    userMapper.registerByEmail(loginId,email,Base64Util.decode(password),timestamp);
                    //注册时新建默认分组
                    userMapper.insertFriendGroupByUserId(userMapper.getUserIdByLoginId(loginId));
                } catch (Exception e) {
                    log.error("registerByEmail",e);
                    throw new ServerException(ResultCode.FAILED);
                }
            }
            //每有一个新用户注册，就提前检测号码池。运行在子线程中，避免阻塞主线程
            numberPoolService.execute();
            return loginId;
        }
        throw new ServerException(ResultCode.INTERNAL_SERVER_ERROR);
    }

    @Override
    public String queryEmailByLoginId(String loginId) {
        return userMapper.queryEmailByLoginId(loginId);
    }

    @Override
    public boolean changePasswordByEmail(String email, String password) {
        try {
            userMapper.changePasswordByEmail(email,Base64Util.decode(password));
        }catch (Exception e){
            e.printStackTrace();
            log.error("changePasswordByEmail",e);
            throw new ServerException(ResultCode.INTERNAL_SERVER_ERROR,false);
        }
        return true;
    }
}
